<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FishingSite;
use App\Server\AliOSS;
use App\Server\Constants;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class FishingSiteController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FishingSite(), function (Grid $grid) {
            $grid->filter(function (Grid\Filter $filter) {
//                $filter->expand();
//                $filter->panel();
                $filter->like('name', '名称');
                $filter->equal('type', '类型')->select(Constants::$siteType);
                $filter->equal('city', '区县')->select(Constants::$city);
                $filter->like('boos', '联系人');
                $filter->like('boos_phone', '电话');
                $filter->equal('home_status', '列表显示')->select([0 => '隐藏', '显示']);
                $filter->equal('map_status', '钓点地图')->select([0 => '隐藏', '显示']);
                $filter->equal('is_host', '热门')->select([0 => '否', '是']);
                $filter->equal('monitor', '监控')->select([0 => '无', '有']);
            });

            $grid->quickSearch('name')->placeholder('钓点名快捷搜索');

            $grid->model()->orderByDesc('id');

            $grid->showActions();
            $grid->disableRowSelector();
            $grid->disableViewButton();
            $grid->scrollbarX();
            $grid->fixColumns(1, -1);

            $grid->column('id', 'ID');
            $grid->column('img')->display(function($img) {
                return AliOSS::getThumbName($img);
            })->image(env('ALIYUN_DOMAIN'), 100);
            $grid->column('name');
            $grid->column('type', '类型')->using((Constants::$siteType));
            $grid->column('city')->display(function($code) {
                return Constants::$city[$code];
            });
            $grid->column('map_latlng', '位置')->display(function ($latlong) {
                $key = env('.TENCENT_MAP_API_KEY');
                return "<a href='https://apis.map.qq.com/uri/v1/geocoder?coord={$latlong}&referer={$key}' target='_blank'>查看地图</a>";
            });
            $grid->column('boos', '联系方式')->display(function ($boos) {
                if (!$boos) {
                    $boos = '未知';
                }
                return $boos . '<br/>' . $this->boos_phone;
            });
            $grid->column('状态')->switchGroup(['home_status' => '首页列表', 'map_status' => '钓点地图', 'is_host' => '热门搜索']);
            $grid->column('tags', '标签')->limit(30)->display(function($tags) {
                if (!$tags) {
                    return '-';
                }
                $tags = explode(',', $tags);
                $html = '';
                foreach ($tags as $k => $tag) {
                    $html .= "<span class='label' style='background:#dda451'>{$tag}</span>&nbsp;";
                    if ($k == 2) {
                        $html .= '<br/>';
                    }
                }
                return $html;
            });
            $grid->column('monitor', '鱼情监控')->switch();
            $grid->column('monitor_img_path', '监控图路径');
//            $grid->column('carousel')->display(function ($pictures) {
//                return json_decode($pictures, true);
//            })->image(env('ALIYUN_DOMAIN'), 100);
//            $grid->column('introduce')->limit(30);
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FishingSite(), function (Form $form) {
            $form->disableViewButton();
            $form->text('name')->required();
            $form->select('city')->options(Constants::$city)->required();
            $form->text('boos');
            $form->text('boos_phone');
            $form->text('tags', '标签')->help('多个标签用英文,号隔开');
            $form->select('type')->options(Constants::$siteType)->required();
            $form->switch('home_status', '首页列表')->default(0)->help('是否在首页钓场列表显示');
            $form->switch('map_status', '钓点地图')->default(1)->help('是否在钓点地图显示');
            $form->switch('is_host', '热门')->default(0)->help('钓点搜索页显示');
            $form->switch('monitor', '鱼情监控')->default(0);
            $form->text('monitor_img_path', '监控图路径');
            $form->text('address', '地址')->required();
            $form->text('map_latlng', '地图坐标')->required()->help("坐标拾取: https://lbs.qq.com/getPoint");
            $form->map('map_lat', 'map_lng', '腾讯地图');
            $form->image('img')->required();
            $form->multipleImage('carousel')->required();
            $form->textarea('introduce')->required();
            $form->saving(function (Form $form) {
                if ($form->img) {
                    $oss = new AliOSS();
                    $oss->createThumb($form->img);
                    if ($form->carousel) {
                        $carousel = explode(',', $form->carousel);
                        foreach ($carousel as $v) {
                            $oss->createBanner($v);
                        }
                    }
                }
                if ($form->map_latlng) {
                    $map = explode(',', $form->map_latlng);
                    $form->map_lat = $map[0];
                    $form->map_lng = isset($map[1]) ? $map[1] : '';
                }
                if ($form->tags) {
                    $form->tags = str_replace("，", ',', $form->tags);
                }
            });
        });
    }
}
